package lib.pacificapp.app;

import java.lang.ref.WeakReference;
  
import lib.pacificapp.util.ICookie;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/***
 * PacificApp Fragment CODE
 * */
public abstract class PaFragment extends Fragment implements ActionInter{
	
	protected View frame;
	
	/***Fragment 页面创建完成*/
	protected abstract void onCreateViewCompleted(View frame);
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		    
		    if (frame == null) {
				frame = inflater.inflate(getLayoutViewId(), null);
				initView(frame);
				onCreateViewCompleted(frame);
		  	} else {
				ViewGroup parent = (ViewGroup) frame.getParent();
				if (parent != null) {
					parent.removeView(frame);
				}
			}
			 return frame;
		 
	}
	@Override
	public abstract int getLayoutViewId();
	@Override
	public abstract int getModeCode();
	@Override
	public abstract boolean initView(View parentView);
	
	/***same width PaFrament this s*/

	 protected boolean text(int rId,String str){
		 TextView tv=(TextView) getActivity().findViewById(rId);
		 if(tv==null)return false;
		 tv.setText(str);
		 return true;
	  }

	//记录用户首次点击返回键的时间
	 private long firstStartTime=0;
 protected void start(Class<? extends Activity> action){
	 long secondTime=System.currentTimeMillis();
     if(secondTime-firstStartTime<1000){
     	 
     }else{
    	 Intent intent = new Intent( getActivity(),action);
	         startActivity(intent);
     }
     firstStartTime=secondTime;
  }
	 
	protected ICookie cookie(String key){
		  return new ICookie(getActivity(),key);
	  }
	  protected  int ALERT=0x0001;
	 
	  
	   @SuppressLint("HandlerLeak")
			Handler msgHandler = new Handler(){
			            @Override
				            public void handleMessage(Message msg){
			            	onSendMessageCompleted(msg.what,msg.obj);
				           }
				 };
	       protected void sendMessage(int code,Object data){
	    	     Message msg=new Message();
	    	           msg.obj=data;
	    	           msg.what=code;
	    	           msgHandler.sendMessage(msg);
	       }	
	       protected void sendMessage(int code){
	    	   sendMessage(code,null);
	       }	
	       protected void onSendMessageCompleted(int code,Object data){
	    	    /**回调*/
	    	    if(code==ALERT){
	    	    	Toast.makeText(this.getActivity(),String.valueOf(data),
	    	    		      Toast.LENGTH_SHORT).show();
	    	    }
	       }
	       
	        
	    
	      @Override
		  public void onDestroy() {  
	           //  If null, all callbacks and messages will be removed.  
	    	   msgHandler.removeCallbacksAndMessages(null);  
	           super.onDestroy();
	       }  
	      
//	      private final Handler mHandler = new MyHandler(this); 
//	       private static class MyHandler extends Handler {  
//	           private final WeakReference<PaFragment> mActivity;  
//	          
//	           public MyHandler(PaFragment activity) {  
//	               mActivity = new WeakReference<PaFragment>(activity);
//	              
//	           }  
//	     
//	           @Override  
//	           public void handleMessage(Message msg) {  
//		         	 // mBack.onComplete(msg);
//	        	    if( mActivity.get()==null)return;
//		        	  mActivity.get().onSendMessageCompleted(msg.what, msg.obj);
//                }  
//	       }  
}
